<h2>XSC</h2>
<?php

require_once('zorba_api.php');


define(CONF_FILE, 'mapping.xml');
define(XSLT_RESULT,'jornada.xml');
define(XSLT_FILE, 'zz_games.xslt');
define(XQUERY_FILE,'game_fixtures.xq');
define(ZZ_GAMES_XML, 'cache/zzxml/zerozero_games.xml');
define(ZZ_HTML, 'cache/zzhtml/zz.html');
define(ZZ_PAGE, 'http://www.zerozero.pt/edicao.php?id_edicao=22951');
define(ZZ_XML, 'cache/zzxml/zerozero.xml');


class XSCrawler {

	private $conf;
	private $zzHtml;
	private $zzXml;
	private $zzGames;
	private $zorba;
	private $store;


	public function __construct() {
		$this->store = InMemoryStore::getInstance();
		$this->zorba = Zorba::getInstance($this->store);

		if( $this->zorbaTest() ){
			echo 'Zorba API Running.<br/>';
		}
		else {
			echo 'Zorba initiation failed!<br/>';
		}
	}

	public function __destruct() {
		$this->zorba->shutdown();
		InMemoryStore::shutdown($this->store);
	}


	private function zorbaTest() {
		$lQuery = $this->zorba->compileQuery('1+1');
		$result = $lQuery->execute();
		$lQuery->destroy();
		return ( strcmp($result,'2') ) ? true : false;
	}
	private function zorbaQuery($query) {
		$lQuery = $this->zorba->compileQuery( $query );
		$result = $lQuery->execute();
		$lQuery->destroy();
		return $result;
	}

	/**
	 * d
	 */
	private function fetch_conf(){
		$this->conf = DOMDocument::loadXML(CONF_FILE)->saveXML();
	}

	/**
	 * d
	 */
	public function fetch_zzHtml() {
		echo 'ZZ HTML<br/>';
		$this->zzPageFile();
		$doc = new DOMDocument();

		$doc->loadHTMLFile( ZZ_HTML );
		echo $doc->saveXML();		
		
		
		
		$fstr = file_get_contents('cache/zzhtml/zerozero.html' );
		echo substr($fstr, 0, strpos($fstr, '<body>') );	
	
		
		$var = file_get_contents( 'zorba_api.php' );	
	
		$doc->normalizeDocument();
		
		$xpathDoc = new DOMXPath( $doc );
		echo 'Starting XSLT<br/>';
		$query = '//table[@class = "lista_jogos"][1]';
	
		$result = $xpathDoc->evaluate( $query ) ;
	
		$newDom = new DOMDocument('1.0','UTF-8');
		$root = $newDom->createElement('jornada');
		$root = $newDom->appendChild($root);
		echo $newDom->saveXML();
		
		foreach ($result as $domElement){
		   $domNode = $newDom->importNode($domElement, true);
		   $root->appendChild($domNode);
		}

		echo 'Wrote: ' . $newDom->save() . ' bytes';

		
		echo 'Complete<br/>';
	}
	/* OK */
	private function zzPageFile() {
		$ch = curl_init( ZZ_PAGE );
		$fp = fopen( ZZ_HTML, 'w');
		curl_setopt($ch, CURLOPT_FILE, $fp);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_VERBOSE, 1);
		curl_exec($ch);
		curl_close($ch);
		fclose($fp);		
	}

	/**
	 * d
	 */
	private function fetch_zzXml() {
		$this->zzXml = DOMDocument::loadXML(ZZ_XML)->saveXML();
	}

	/**
	 * d
	 */
	private function fetch_zzGames() {
		$this->zzGames = DOMDocument::loadXML(ZZ_GAMES_XML)->saveXML();
	}

	/**
	 * transforms zz Games File
	 */
	public function transform_zzGamesFile() {
		$this->zzPageFile();
		$xslt = new XSLTProcessor();
		$xsltDoc = new DOMDocument();
		$xsltDoc->load( XSLT_FILE );
		$xslt->importStyleSheet( $xsltDoc );
		$xmlDoc = new DOMDocument();
		$xmlDoc->load( ZZ_GAMES_XML );
		$resultXml = $xslt->transformToXML( $xmlDoc );
		if( !file_put_contents(XSLT_RESULT, $resultXml) ) {
			echo 'ERROR: Saving '.XSLT_RESULT.'!<br/>';
		}
	}

	/**
	 * writes the file jornada_e_x.xml on disk
	 */
	private function write_file($e='2011-2012', $x) {

	}

	/**
	 * associates keys from the zz and xs
	 * XQueryX Facility Update
	 */
	private function associate_games() {
		$xquery = file_get_contents( XQUERY_FILE );
		$this->zorbaQuery();
	}

	private function fetch_files(){
		$this->fetch_conf();
		$this->fetch_zzHtml();
		$this->fetch_zzXml();
		$this->fetch_zzGames();
	}

}

echo '<h3>Running XSCrawler</h3>';
$xscrawler = new XSCrawler();
$xscrawler->transform_zzFiles();
$xscrawler->associate_games();